package com.xingmei.custom.linked;


public class LinkedList {

    Node node;

    public LinkedList(int value) {
        node = new Node(value);
    }

    // 给该链表添加节点数据
    public void append(int value) {
        Node currNode = node;
        while (true) {
            Node nextNode = currNode.next;
            if (nextNode == null) {
                nextNode = new Node(value, currNode, null);
                currNode.next = nextNode;
                break;
            }
            currNode = nextNode;
        }
    }

    // 该链表合并另外一个链表
    public void mergedList(LinkedList targetLinkedList) {
        Node targetCurrNode = targetLinkedList.node;
        while (true) {
            this.append(targetCurrNode.value);
            if (targetCurrNode.next == null) {
                break;
            }
            targetCurrNode = targetCurrNode.next;
        }
    }

    // 排序链表--冒泡排序
    public void sort(Sort sort) {
        Node outLoopNode = node;
        while (true) {
            Node innerLoopNode = outLoopNode.next;
            if (innerLoopNode == null) {
                break;
            }
            Node tempNextNode = innerLoopNode;
            do {
                if (sort == Sort.ASC) {
                    if (outLoopNode.value > innerLoopNode.value) {
                        int tmp = outLoopNode.value;
                        outLoopNode.value = innerLoopNode.value;
                        innerLoopNode.value = tmp;
                    }
                } else if (sort == Sort.DESC) {
                    if (outLoopNode.value < innerLoopNode.value) {
                        int tmp = outLoopNode.value;
                        outLoopNode.value = innerLoopNode.value;
                        innerLoopNode.value = tmp;
                    }
                }
                innerLoopNode = innerLoopNode.next;
            } while (innerLoopNode != null);
            outLoopNode = tempNextNode;
        }
    }

   public static class Node {
        int value;
        Node pre;
        Node next;
        public Node(int value) {
            this.value = value;
        }
        public Node(int value, Node pre, Node next) {
            this.value = value;
            this.pre = pre;
            this.next = next;
        }
    }

    public enum Sort{
        ASC("asc"),
        DESC("desc");

         final String order;

         Sort(String order) {
            this.order = order;
        }
    }

}
